const {
    VersionedTransaction
} = require('@solana/web3.js')

var ed25519 = require('@noble/curves/ed25519');

async function mpcSign(message, secretKey) {
    return  ed25519.ed25519.sign(message, secretKey.slice(0, 32));
}

class MpcVersionedTransaction extends VersionedTransaction {
    constructor(message){
        super(message);
    }
    async  sign(signers){
        console.log('in MpcVersionTransaction');
        //super.sign(signers);
        
        const messageData = this.message.serialize();
        const signerPubkeys = this.message.staticAccountKeys.slice(0, this.message.header.numRequiredSignatures);
        for (const signer of signers) {
          const signerIndex = signerPubkeys.findIndex(pubkey => pubkey.equals(signer.publicKey));
          if(signerIndex === -1){ 
                console.log(`Cannot sign with non signer key ${signer.publicKey.toBase58()}`)
            };
          this.signatures[signerIndex] = await mpcSign(messageData, signer.secretKey);
        }
        
    }
}

module.exports = MpcVersionedTransaction;